<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Group extends Model
{
//    使用软删
    use \think\model\concern\SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $pk='id';
    protected $table='weekoneExam_groups';

    public function powers(){
//        模型关联
        return $this->hasMany(Power::class,'id','power_id');
    }

    public function getPower(){
//        这是关联方法
        return $this->with('powers')->select()->toArray();
    }

    public function addGroupData($data){
//        添加一条数据
        return $this->insert($data);
    }


    public function getGroupData($keywords){
//        根据关键字搜索数据并分页
        return $this->field('id,group_name,state')
            ->where('group_name','like',"%$keywords%")
            ->paginate([
                'list_rows'=>10,
                'query'=>['keywords'=>$keywords]
            ]);
    }


    public function delGroupAll($id){
//        批量删除
        return $this->destroy($id);
    }


    public function delGroup($id){
//        单个删除
        return $this->destroy($id);
    }

    public function someExport(){
//        导出数据
        return $this->field('id,group_name')->select();
    }

    public function allExport(){
//        导出数据
        return $this->field('id,group_name,state')->select();
    }

    public function getGroupById($id){
//        查询单条数据
        return $this->find($id);
    }

    public function updateGroupData($data){
//        修改数据
        return $this->update(['group_name'=>$data['group_name'],'state'=>$data['state'],'power_id'=>$data['power_id'],'id'=>$data['id']]);
    }
}
